package cn.itcast.mq.listener;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
public class SpringRabbitListener1 {
    /**
     * 基本消息对列
     * @param msg
     */
    @RabbitListener(queues = "basic.queue")
    public void listenerQueue(String msg){
        System.out.println("接收到的Basic消息:"+msg);
    }

    /**
     * 工作消息对列
     * @param msg
     */
    @RabbitListener(queues = "word.queue")
    public void listenerWordQueue1(String msg) throws InterruptedException {
        System.out.println("接收到的Work消息1:"+msg);
        Thread.sleep(1000);
    }
    @RabbitListener(queues = "word.queue")
    public void listenerWordQueue2(String msg) throws InterruptedException {
        System.out.println("接收到的Work消息2:"+msg);
        Thread.sleep(2000);
    }

    /**
     * 发布订阅之广播
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = "itcast.fanout",type = ExchangeTypes.FANOUT),
            value = @Queue(name = "fanout.queue1")
    ))
    public void listenerFanoutQueue1(String msg){
        System.out.println("接收到的fanout消息1:"+msg);
    }

    /**
     * 发布订阅之广播
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange =@Exchange(name = "itcast.fanout",type = ExchangeTypes.FANOUT),
            value = @Queue(name = "fanout.queue2")
    ))
    public void listenerFanoutQueue2(String msg){
        System.out.println("接收到的fanout消息2:"+msg);
    }

    /**
     * 发布订阅之路由
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = "itcast.direct",type=ExchangeTypes.DIRECT),
            value = @Queue(name = "direct.queue1"),
            key = {"aa","bb","cc"}
    ))
    public void listDirectQueue1(String msg){
        System.out.println("接收到的direct消息1:"+msg);
    }

    /**
     * 发布订阅之路由
     * @param msg
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            value = @Queue(name = "direct.queue2"),
            key = {"aaa","bbb","ccc"}
    ))
    public void listDirectQueue2(String msg){
        System.out.println("接收到的direct消息2:"+msg);
    }

    /**
     * 发布订阅之主题
     */
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            value = @Queue(name = "topic.queue1"),
            key = "aa.#"
    ))
    public void listToicQueue1(String msg){
        System.out.println("消费者1接收到的topic消息:"+msg);
    }

    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            value = @Queue(name = "topic.queue2"),
            key = "#.bb"
    ))
    public void listToicQueue2(String msg){
        System.out.println("消费者2接收到的topic消息:"+msg);
    }
}
